package org.linlinjava.litemall.admin.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.admin.dto.GoodsAllinone;
import org.linlinjava.litemall.admin.vo.CatVo;
import org.linlinjava.litemall.core.qcode.QCodeService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.dao.IliquormallTopicMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.linlinjava.litemall.admin.util.AdminResponseCode.GOODS_NAME_EXIST;

@Service
public class AdminLiquorTopicService {
    private final Log logger = LogFactory.getLog(AdminLiquorTopicService.class);

    @Autowired
    private ILiquormallTopicService liquorTopicService;

    public Object list(String title, Integer page, Integer limit, String sort, String order) {
        List<IliquormallTopic> liquorTopicList = liquorTopicService.queryLiquorTopicList(title, page, limit, sort, order);
        return ResponseUtil.okList(liquorTopicList);
    }

    /**
     * 校验酒业咨询合法性
     *
     * @param liquorTopic
     * @return
     */
    private Object validate(IliquormallTopic liquorTopic) {
        if (StringUtils.isEmpty(liquorTopic.getTitle())) {
            return ResponseUtil.badArgument();
        }

        return null;
    }

    /**
     * 删除酒业资讯
     *
     * @param liquorTopic
     * @return
     */
    @Transactional
    public Object delete(IliquormallTopic liquorTopic) {
        Integer id = liquorTopic.getId();

        if (id == null) {
            return ResponseUtil.badArgument();
        }

        if (liquorTopicService.deleteLiquorTopic(id) == 0) {
            return ResponseUtil.fail();
        }

        return ResponseUtil.ok();

    }

    /**
     * 添加酒业资讯
     *
     * @param liquorTopic
     * @return
     */
    @Transactional
    public Object create(IliquormallTopic liquorTopic) {
        Object error = validate(liquorTopic);

        if (error != null) {
            return error;
        }

        liquorTopic.setReadCount("0");
        if (liquorTopicService.insertLiquorTopic(liquorTopic) == 0) {
            logger.error(liquorTopic.toString(), new RuntimeException("更新数据失败"));
            return ResponseUtil.fail();
        }

        return ResponseUtil.ok();
    }

    /**
     * 更新酒业资讯
     *
     * @param liquorTopic
     * @return
     */
    @Transactional
    public Object update(IliquormallTopic liquorTopic) {
        Object error = validate(liquorTopic);

        if (error != null) {
            return error;
        }

        if (liquorTopicService.updateLiquorTopic(liquorTopic) == 0) {
            logger.error(liquorTopic.toString(), new RuntimeException("更新数据失败"));
            return ResponseUtil.fail();
        }

        return ResponseUtil.ok();
    }

    /**
     * 返回酒业资讯详情
     *
     * @param id
     * @return
     */
    public Object detail(Integer id) {

        IliquormallTopic liquorTopic = liquorTopicService.selectById(id);

        Map<String, Object> data = new HashMap<>();
        data.put("liquorTopic", liquorTopic);

        return ResponseUtil.ok(data);
    }

}
